<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="/WEB-INF/tag/c.tld"%>
<div id="dbConfigTableEdit">
<section class="panel">
	<div class="panel-body">
		<form action="${webRoot}/dbConfig/table/save" id="formTableInfo" class="form-horizontal">
			<input type="hidden" name="code" value="${project.projectCode}"/>
			<input type="hidden" name="id" value="${table.tableName }"/>
			<div class="form-group">
		    	<label class="col-sm-1 control-label">表名</label>
		    	<div class="col-md-5">
		    	<input type="text" class="form-control" name="tableName" value="${table.tableName }" <c:if test="${table.isExist==true}">readOnly</c:if>/>
		    	</div>
		    	<label class="col-sm-1 control-label" style="padding-right:5px;padding-left:5px;">中文名</label>
		    	<div class="col-md-5">
		    	<input type="text" class="form-control" name="tableChinaName" value="${table.tableChinaName }"/>
		    	</div>
		  	</div>
		  	<div class="form-group">
		    	<label class="col-sm-1 control-label">类名</label>
		    	<div class="col-md-5">
		    	<input type="text" class="form-control" name="className" value="${table.className }"/>
		    	</div>
		    	<div class="col-md-6">
		    		<button type="button" class="btn btn-default" id="btnSaveTable">保存</button>
		    	</div>
		  	</div>
		</form>
	</div>
</section>
<section class="panel">
	<div class="panel-heading clearfix">
		<div class="pull-right">
			<button class="btn btn-success btn-sm" id="btnAddTableField">新增字段</button>
		</div>
	</div>
	<div class="panel-body">
	<table class="table table-bordered table-hover" id="tableField">
		<thead><tr>
			<th data-code="code">字段代码</th><th data-code="name">字段名称</th>
			<th data-code="type">字段类型</th><th data-code="length">字段长度</th>
			<th data-code="isKey">是否是主键</th><th data-code="isNullable">是否可为空</th>
			<th data-code="operate" style="width:80px;">操作</th>
		</tr></thead>
	</table>
	</div>
</section>
<script type="text/javascript">
$(function(){
	$('#tableField').jfTable({
		pagingEl:null,
		ajax:{
			url:'${webRoot}/dbConfig/table/field',
			param:function(param){
				return {
					code:'${project.projectCode}',
					tableName:$('#formTableInfo input[name="id"]').val()
				};
			}
		},
		cols:{
			operate:{
				icons:[{
					tooltip:'编辑',
					icon:'fa-edit',
					click:function(record){
						var tableName = $('#formTableInfo input[name="id"]').val();
						jf.modal('修改表字段','${webRoot}/dbConfig/table/field/edit',
							{code:'${project.projectCode}',tableName:tableName,fieldName:record.code},function(result){
								if(result.error==1){$('#tableField').jfTable('loadData');$(this).modal('hide');}
								else{jf.error(result.msg);}
						});
					}
				},{
					tooltip:'删除',
					icon:'fa-trash-o',
					click:function(record){
						jf.alert('表字段删除提示','确定要否删除字段'+record.code+"吗?",function(flag){
							if(flag==true){
								var tableName = $('#formTableInfo input[name="id"]').val();
								$.getJSON('${webRoot}/dbConfig/table/field/del',
									{code:'${project.projectCode}',tableName:tableName,fieldName:record.code},function(result){
										if(result.error==1){$('#tableField').jfTable('loadData');$(this).modal('hide');}
										else{jf.error(result.msg);}
								});
							}
						});
					}
				}]
			}
		}
	});
	
	$('#btnSaveTable').click(function(){
		$('#formTableInfo').jfForm('submit',function(result){
			if(result.error==1){
				$('#divDbTree').jfTree('refreshLoad','news');
				$('#formTableInfo input[name="id"]').val(result.data.tableName);
				jf.info("保存成功");
			}else{jf.error(result.msg);}
		});
	});
	
	$('#btnAddTableField').click(function(){
		var tableName = $('#formTableInfo input[name="id"]').val();
		if(!tableName){jf.info("请先保存表信息再编辑字段");}else{
			jf.modal('添加表字段','${webRoot}/dbConfig/table/field/edit',
				{code:'${project.projectCode}',tableName:tableName},function(result){
					if(result.error==1){$('#tableField').jfTable('loadData');$(this).modal('hide');}
					else{jf.error(result.msg);}
			});}
	});
})
</script>
</div>